package com.link.base.base.member.service;

import com.link.base.base.member.dao.mybatis.mapper.GiftCardWxMapper;
import com.link.base.base.member.model.GiftCardPage;
import com.link.base.base.member.model.GiftCardWx;
import com.link.base.wechat.wxcard.service.WxCardInfoService;
import com.link.base.wechat.wxplatform.model.WxPlatform;
import com.link.base.wechat.wxplatform.service.WxPlatformService;
import com.link.base.wechat.wxtoken.service.WxTokenService;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 货架打通小程序
 *
 * @author 路江
 * @date 2019/03/04 18:01
 */
@Service
public class GiftCardWxServiceImpl extends BasicServiceImpl<GiftCardWx> implements GiftCardWxService {

    @Resource
    private GiftCardWxMapper giftCardWxMapper;

    @Resource
    private WxPlatformService wxPlatformService;

    @Resource
    private WxTokenService wxTokenService;

    @Resource
    private GiftCardPageService giftCardPageService;

    @Resource
    private WxCardInfoService wxCardInfoService;


    
    @Override
    public BasicMapper<GiftCardWx> getBasicMapper() {
        return giftCardWxMapper;
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public Map<String, Object> setGiftCardWxa(GiftCardWx giftCardWx) throws Exception {
        this.insert(giftCardWx);
        GiftCardPage cardPage = new GiftCardPage();
        cardPage.setId(Long.parseLong(giftCardWx.getPageId()));
        cardPage = giftCardPageService.queryById(cardPage);
        cardPage.setMpId(giftCardWx.getMpId());
        giftCardPageService.update(cardPage);

        WxPlatform wxPlatform = new WxPlatform();
        wxPlatform.setId(cardPage.getWxId());
        wxPlatform = wxPlatformService.queryById(wxPlatform);
        giftCardWx.setPageId(cardPage.getPageId());
        return wxCardInfoService.setGiftCardWxa(giftCardWx,wxPlatform.getWx_app_id());
    }
}

